package org.openatom.ubml.model.vo.definition.json.element;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.openatom.ubml.model.common.definition.cef.json.SerializerUtils;
import org.openatom.ubml.model.common.definition.commonmodel.entity.GspCommonElement;
import org.openatom.ubml.model.common.definition.commonmodel.json.element.CmElementDeserializer;
import org.openatom.ubml.model.vo.definition.GspViewModelElement;
import org.openatom.ubml.model.vo.definition.collection.VMActionCollection;
import org.openatom.ubml.model.vo.definition.common.VMHelpConfig;
import org.openatom.ubml.model.vo.definition.common.mapping.GspVoElementMapping;
import org.openatom.ubml.model.vo.definition.json.ExtendPropertiesDeserializer;
import org.openatom.ubml.model.vo.definition.json.ViewModelJsonConst;
import org.openatom.ubml.model.vo.definition.json.mapping.GspVoElementMappingDeserializer;
import org.openatom.ubml.model.vo.definition.json.operation.VmActionCollectionDeserializer;
/**
 * The  Josn Deserializer Of View Model Element
 *
 * @ClassName: ViewElementDeserializer
 * @Author: Benjamin Gong
 * @Date: 2021/1/11 17:13
 * @Version: V1.0
 */
public class ViewElementDeserializer extends CmElementDeserializer {
    @Override
    protected GspCommonElement createElement() {
        return new GspViewModelElement();
    }

    @Override
    protected boolean readExtendElementProperty(GspCommonElement item, String propName, JsonParser jsonParser) {
        boolean result = true;
        GspViewModelElement field = (GspViewModelElement) item;
        switch (propName) {
            case ViewModelJsonConst.IsBeckendOnly:
                field.setIsBeckendOnly(SerializerUtils.readPropertyValue_boolean(jsonParser));
                break;
            case ViewModelJsonConst.Mapping:
                readMapping(jsonParser, field);
                break;
            case ViewModelJsonConst.HelpActions:
                readHelpActions(jsonParser, field);
                break;
            case ViewModelJsonConst.ImmediateSubmission:
                field.setImmediateSubmission(SerializerUtils.readPropertyValue_boolean(jsonParser));
                break;
            case ViewModelJsonConst.IsVirtualViewElement:
                field.setIsVirtualViewElement(SerializerUtils.readPropertyValue_boolean(jsonParser));
                break;
            case ViewModelJsonConst.EnableMultiLanguageInput:
                field.setEnableMultiLanguageInput(SerializerUtils.readPropertyValue_boolean(jsonParser));
                break;
            case ViewModelJsonConst.ShowInFilter:
                field.setShowInFilter(SerializerUtils.readPropertyValue_boolean(jsonParser));
                break;
            case ViewModelJsonConst.ShowInSort:
                field.setShowInSort(SerializerUtils.readPropertyValue_boolean(jsonParser));
                break;
            case ViewModelJsonConst.ExtendProperties:
                readExtendProperties(jsonParser, field);
                break;
            case ViewModelJsonConst.VMHelpConfig:
                readVMHelpConfig(jsonParser, field);
                break;
            default:
                result = false;
                break;
        }
        return result;
    }

    private void readHelpActions(JsonParser jsonParser, GspViewModelElement field) {
        VmActionCollectionDeserializer deserializer = new VmActionCollectionDeserializer();
        VMActionCollection collection = deserializer.deserialize(jsonParser, null);
        field.setHelpActions(collection);
    }

    private void readMapping(JsonParser jsonParser, GspViewModelElement field) {
        GspVoElementMappingDeserializer deserializer = new GspVoElementMappingDeserializer();
        GspVoElementMapping mapping = (GspVoElementMapping) deserializer.deserialize(jsonParser, null);
        field.setMapping(mapping);
    }

    private void readExtendProperties(JsonParser jsonParser, GspViewModelElement field) {
        ExtendPropertiesDeserializer deserializer = new ExtendPropertiesDeserializer();
        field.setExtendProperties(deserializer.deserialize(jsonParser, null));
    }

    private void readVMHelpConfig(JsonParser jsonParser, GspViewModelElement field) {
        VMHelpConfig config = new VMHelpConfig();
        SerializerUtils.readStartObject(jsonParser);
        if (jsonParser.getCurrentToken() == JsonToken.FIELD_NAME) {
            while (jsonParser.getCurrentToken() == JsonToken.FIELD_NAME) {
                readVMHelpConfig(jsonParser, config);
            }
        }
        SerializerUtils.readEndObject(jsonParser);
        field.setVMHelpConfig(config);
    }

    private void readVMHelpConfig(JsonParser jsonParser, VMHelpConfig config) {
        String propName = SerializerUtils.readPropertyName(jsonParser);
        switch (propName) {
            case ViewModelJsonConst.HelpId:
                config.setHelpId(SerializerUtils.readPropertyValue_String(jsonParser));
                break;
        }
    }
}
